package com.ruoyi.command.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.command.domain.DefectCases;
import com.ruoyi.command.service.IDefectCasesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 缺陷用例Controller
 *
 * @author ruoyi
 * @date 2024-03-12
 */
@RestController
@RequestMapping("/command/cases")
public class DefectCasesController extends BaseController
{
    @Autowired
    private IDefectCasesService defectCasesService;

    /**
     * 查询缺陷用例列表
     */
    @PreAuthorize("@ss.hasPermi('command:cases:list')")
    @GetMapping("/list")
    public TableDataInfo list(DefectCases defectCases)
    {
        startPage();
        List<DefectCases> list = defectCasesService.selectDefectCasesList(defectCases);
        return getDataTable(list);
    }

    /**
     * 导出缺陷用例列表
     */
    @PreAuthorize("@ss.hasPermi('command:cases:export')")
    @Log(title = "缺陷用例", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DefectCases defectCases)
    {
        List<DefectCases> list = defectCasesService.selectDefectCasesList(defectCases);
        ExcelUtil<DefectCases> util = new ExcelUtil<DefectCases>(DefectCases.class);
        util.exportExcel(response, list, "缺陷用例数据");
    }

    /**
     * 获取缺陷用例详细信息
     */
    @PreAuthorize("@ss.hasPermi('command:cases:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(defectCasesService.selectDefectCasesById(id));
    }

    /**
     * 新增缺陷用例
     */
    @PreAuthorize("@ss.hasPermi('command:cases:add')")
    @Log(title = "缺陷用例", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DefectCases defectCases)
    {
        // 设置缺陷用例的创建人为当前登录用户
        defectCases.setCreateBy(getUsername());
        return toAjax(defectCasesService.insertDefectCases(defectCases));
    }

    /**
     * 修改缺陷用例
     */
    @PreAuthorize("@ss.hasPermi('command:cases:edit')")
    @Log(title = "缺陷用例", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DefectCases defectCases)
    {
        defectCases.setUpdateBy(getUsername());
        return toAjax(defectCasesService.updateDefectCases(defectCases));
    }

    /**
     * 删除缺陷用例
     */
    @PreAuthorize("@ss.hasPermi('command:cases:remove')")
    @Log(title = "缺陷用例", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(defectCasesService.deleteDefectCasesByIds(ids));
    }
}
